<?php

namespace app\api\controller;

use app\api\controller\user\Site;
use app\api\model\store\Shop as ShopModel;
use think\Db;
use app\common\service\qrcode\Shop as ShopQRcode;


/**
 * 门店列表
 * Class Shop
 * @package app\api\controller
 */
class Shop extends Controller
{
    /**
     * 门店列表
     * @param string $longitude
     * @param string $latitude
     * @return array
     * @throws \think\exception\DbException
     */
    public function lists($longitude = '', $latitude = '')
    {
        $model = new ShopModel;
        $list = $model->getList(true, $longitude, $latitude);
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 门店详情
     * @param $shop_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function detail($shop_id)
    {
        $detail = ShopModel::detail($shop_id);
        return $this->renderSuccess(compact('detail'));
    }

    /**
     * 门店列表
     * 根据用户选择门店更新user表中门店id
     * @return array
     * @throws \think\exception\DbException
     */
    public function selectShop()
    {
        $shop_id = $this->request->post('shop_id');
        if(empty($shop_id)){
            return $this->renderError('选择站点失败');
        }
        // 用户信息
        $user = $this->getUser();
        //更新/绑定 用户shop_id
       $data = Db::table('yoshop_user')->where(['user_id'=>$user['user_id']])
            ->update(['shop_id'=>$shop_id,'update_time'=>time()]);
       if(!$data){
           return $this->renderError('更新站点失败');
       }
        return $this->renderSuccess(compact('shop_id','user'));
    }

    /**
     * 获取门店二维码
     * @param $shop_id
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function shopQrcode()
    {
        $shop_id = $this->request->param('shop_id');
//        // 订单详情
//        $order = OrderModel::getUserOrderDetail($order_id, $this->user['user_id']);
//        // 判断是否为待核销订单
//        if (!$order->checkExtractOrder($order)) {
//            return $this->renderError($order->getError());
//        }
        $Qrcode = new ShopQRcode(
            $this->wxapp_id,
            $shop_id
        );
        return $this->renderSuccess([
            'qrcode' => $Qrcode->getImage(),
        ]);
    }


    /**
     * 门店详情
     * @param $shop_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function shopname()

    {
        $shop_id = $this->request->param('shop_id');
        //更新/绑定 用户shop_id
        $data = Db::name('store_shop')->where(['shop_id'=>$shop_id,'is_delete'=>0,'status'=>1])
           ->field('shop_name')->find();
        if(!$data){
            return $this->renderError('请重新选择站点');
        }
        return $this->renderSuccess(compact('data'));
    }


}